package org.wyn.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.wyn.common.ResultType;
import org.wyn.mongodb.bean.User;
import org.wyn.service.IRegisterSerivce;

import com.google.inject.Inject;
/**
 * 用户注册action
 * @author wyn
 *
 */
@Namespace("/register")
@ParentPackage("unSecurity")
public class RegisterAction extends WynActionSupport{
	
	/**
	 * 输入跳转
	 */
	@Action(value="input",results = {@Result(name = INPUT, type = ResultType.FREEMARKER, location = "register_input.ftl")})
	public String input(){
		return INPUT;
	}
	/**
	 * 注册
	 * @return
	 */
	@Action(value="register",results = {
			@Result(name = INPUT, type = ResultType.FREEMARKER, location = "register_input.ftl")
			,@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "register_success.ftl")
			})
	public String register(){
		if(iRegisterSerivce.userExist(userName))//用户已存在
			//TODO 在userName已存在的时候提示可以直接登录 
			return INPUT;
		User user = new User();
		user.setUserName(userName);
		user.setPassword(password);
		user.setRealName(realName);
		user.setGender(gender);
		user.setAddTime(System.currentTimeMillis());
		if(iRegisterSerivce.registerNewUser(user)){
			//注册成功
			return SUCCESS;
		}else{
			//注册失败，回到输入页面
			return INPUT;
		}
	}
	
	private static final long serialVersionUID = 1L;
	
	private String userName;
	private String password;
	private String realName;
	private String gender;
	
	/**
	 * 用户注册服务类
	 */
	@Inject
	private IRegisterSerivce iRegisterSerivce = null;

	public IRegisterSerivce getiRegisterSerivce() {
		return iRegisterSerivce;
	}
	public void setiRegisterSerivce(IRegisterSerivce iRegisterSerivce) {
		this.iRegisterSerivce = iRegisterSerivce;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	
}
